package com.source.adnroid.comm.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.source.adnroid.comm.ui.adapter.ChatMessageAdapter;
import com.source.adnroid.comm.ui.chatutils.KeybordUtils;
import com.source.android.androidcomm.R;
import java.util.ArrayList;
public abstract class ChatBaseFragment extends Fragment {
    private String TAG = "ChatBaseFragment";
    SwipeToLoadLayout swipeToLoadLayout;//刷新 加载 控件
    RecyclerView recyclerView;//聊天信息控件
    ChatMessageAdapter chatMessageAdapter;//聊天信息适配器
    EditText chatEdit;
    TextView componentShowButton;//最下方组件显示按钮
    LinearLayout componentContent;//最下方组件显示控件
    LinearLayoutManager linearLayoutManager;
    TextView msgNotRead;//未读消息显示控件
    View view;
    ArrayList list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_chat, container, false);
        initHistoryData();
        initView();
        initListener();
        updateData();
        return view;
    }
    public abstract void initHistoryData();//加载历史数据
    public abstract void updateData();//加载当前数据
    public abstract void initListener();//加载上拉 下拉监听器
    public abstract void onKeyListener();//加载上拉 下拉监听器
    private void initView() {
        msgNotRead=view.findViewById(R.id.msg_not_read);
        msgNotRead.setOnClickListener(new OnClickListener());
        componentShowButton = view.findViewById(R.id.component_button_show);
        componentShowButton.setOnClickListener(new OnClickListener());
        componentContent=view.findViewById(R.id.component_button_content);
        chatEdit = view.findViewById(R.id.chatEdit);
        chatEdit.setOnKeyListener(new OnKeyListener());
        swipeToLoadLayout = view.findViewById(R.id.swipeToLoadLayout);
        recyclerView = view.findViewById(R.id.swipe_target);
        recyclerView.setOnTouchListener(new OnToucheListener());
        //为recyclerView设置布局管理器
        linearLayoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        chatMessageAdapter = new ChatMessageAdapter(list);
        //为recyclerView设置适配器
        recyclerView.setAdapter(chatMessageAdapter);
    }
    public class OnToucheListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Log.d(TAG, "On RecycleView Touch");
                clickKeyBord();
            }
            return false;
        }
    }

    public class OnClickListener implements View.OnClickListener {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
            Log.d(TAG, v.getId()+" clicked");
            if(v.getId()==R.id.component_button_show){
                clickKeyBord();
                Log.d(TAG, "VISIBLE"+ componentContent.getVisibility());
                if( componentContent.getVisibility()==componentContent.VISIBLE){
                    componentContent.setVisibility(View.GONE);
                }else if( componentContent.getVisibility()==componentContent.GONE){
                    componentContent.setVisibility(View.VISIBLE);
                }
            }else if(v.getId()==R.id.msg_not_read){
                recyclerView.scrollToPosition(chatMessageAdapter.getItemCount() - 1);
                msgNotRead.setText("");
            }
        }
    }
    public  class OnKeyListener implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode==KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_DOWN){
                Log.i(TAG,"KEYCODE_ENTER click");
                onKeyListener();
                return true;
            }
            return false;
        }
    }
    public void clickKeyBord(){
        if (KeybordUtils.isSoftInputShow(getActivity())) {
            KeybordUtils.closeKeybord(chatEdit, getActivity());
        }
    }
    @Override
    public void onDetach() {
        super.onDetach();
    }


}
